<?php

/**
 * Gallery Base Functions
 * 
 * @package Bd Gallery
 * @author Bebliuc George Cristian
 * @author Gianfranco Liporace
 * @copyright 2008-2010
 * @version 1.3
 * @access public
 * @permisions Modify, Redistribute but leave the credits alive :P
 * @use Noncommercial, for commercial use, please contact me at bebliuc.george@gmail.com, it's for free, but I need to know
 * 
 */

Plugin::setInfos(array(
	'id'          => 'gallery',
	'title'       => 'Bd Gallery',
	'description' => 'This plugin provides a photo album to store your pictures on your site.',
	'version'     => '1.2',
	'license'     => 'MIT',
	'author'      => 'Bebliuc George',
	'website'     => 'http://frog.bebliuc.ro/',
	'update_url'  => 'http://frog.bebliuc.ro/updates.xml',
	'require_wolf_version' => '0.6',
    //required to grant frontend access to admin functions
    'type'        => 'both'
));

define('GALLERY_ROOT', URI_PUBLIC . 'wolf/plugins/gallery');

// include models
AutoLoader::addFile('Category', CORE_ROOT.'/plugins/gallery/models/Category.php');
AutoLoader::addFile('Photos', CORE_ROOT.'/plugins/gallery/models/Photos.php');

Plugin::addController('gallery', 'Gallery');

$gallery_path = '/' . Plugin::getSetting('gallery_page', 'gallery');
Dispatcher::addRoute(array(
                    $gallery_path  => '/plugin/gallery/view/',
                    $gallery_path . '/' => '/plugin/gallery/view/',
                    $gallery_path . '/view_photo/:num' => '/plugin/gallery/view_photo/$1',
                    $gallery_path . '/:num' => '/plugin/gallery/view/$1',
                    $gallery_path . '/tags/:any' => '/plugin/gallery/tags/$1'));


/**
 * gallery_js
 * Include javascript library for gallery animation effects based on user's settings
 * Put this into your preferred layout <head>'s tag: <?php gallery_js() ?>
 */
function gallery_js() {

    $settings = Plugin::getAllSettings('gallery');

    $script_path = GALLERY_ROOT . "/scripts/";

    if ($settings['script'] == "shadowbox") {
        if ($settings['framework'] == 1) {
            print "<script type=\"text/javascript\" src=\"{$script_path}shadowbox/jquery-1.2.6.min.js\"></script>";
        }
        print "<script type=\"text/javascript\" src=\"{$script_path}shadowbox/shadowbox-2.0.js\"></script>";
        print "<script type=\"text/javascript\">
	
Shadowbox.loadSkin('classic', '{$script_path}shadowbox/src/skin/'); 
Shadowbox.loadLanguage('en', '{$script_path}shadowbox/src/lang/'); 
Shadowbox.loadPlayer(['img', 'qt'], '{$script_path}shadowbox/src/player/');

window.onload = Shadowbox.init;
		</script>";
    }
    if ($settings['script'] == "popup") {
        echo "<script language=\"JavaScript\"><!--
function openWin(img) {
  winId = window.open('','newwin','width=1024,height=768');
  winId.document.write('<body onLoad=\"if (window.focus) window.focus()\"><center>');
  winId.document.write('<img src=\"' + img + '\">');
  winId.document.write('</center></body>');
  winId.document.close();
}
//--></script>
";

    }
    if ($settings['script'] == "slimbox") {
        if ($settings['framework'] == 1) {
            echo "<script type=\"text/javascript\" src=\"{$script_path}slimbox/js/mootools.js\"></script>";
        }
        echo "<script type=\"text/javascript\" src=\"{$script_path}slimbox/js/slimbox.js\"></script>";
        echo "<link rel=\"stylesheet\" href=\"{$script_path}slimbox/css/slimbox.css\" type=\"text/css\" media=\"screen\" />";
    }

}

/**
 * random_photo_fx()
 * Display a random photo thumbnail which if clicked, displays the bigger one with js animation effects.
 */

function random_photo_fx($tags=null) {
    $settings = $settings = Plugin::getAllSettings('gallery');
    $page = $settings['gallery_page'];
    $result_final = "";
    $image_url = URL_PUBLIC . "public/" . $settings['images_folder'];

    $PDO = Record::getConnection();
    $driver = strtolower($PDO->getAttribute(Record::ATTR_DRIVER_NAME));
    if ($driver == 'sqlite') {
        $rand_func = 'random()';
    } else {
        // should work with mysql and postgres
        $rand_func = 'rand()';
    }
    if ($tags != null) {
        $where = "WHERE photo_category in (SELECT id from " . TABLE_PREFIX . "category WHERE tags in (" . my_implode($tags) . "))";
    } else {
        $where = '';
    }

    $sql = "SELECT * FROM " . TABLE_PREFIX . "photos " . $where . " ORDER BY " . $rand_func;
    $stmt = $PDO->prepare($sql);
    $stmt->execute();

    $photo = $stmt->fetchObject();

    if ($settings['script'] == "shadowbox") {
        $result_final = "
					<a href='{$image_url}{$photo->photo_filename}' rel=\"shadowbox;options={handleOversize:'drag'}\" 
					title=\"{$photo->photo_title} - {$photo->photo_caption}\"><img src='{$image_url}/tb_{$photo->photo_filename}' border='0' alt='{$photo->photo_title}' class=\"thumbnail\"/></a>";
    } elseif ($settings['script'] == "none") {
        $result_final = "<a href='{$setting->gallery_page}?cid={$photo->category}&pid={$photo->photo_id}'><img src='{$image_url}/tb_{$photo->photo_filename}' border='0' alt='{$photo->photo_title}' class=\"thumbnail\" /></a>";
    } elseif ($settings['script'] == "slimbox") {
        $result_final = "
					<a href='{$image_url}{$photo->photo_filename}' rel=\"lightbox\" title=\"{$photo->photo_title} - {$photo->photo_caption}\"><img src='{$image_url}/tb_{$photo->photo_filename}' border='0' alt='{$photo->photo_title}' class=\"thumbnail\"/></a>";
    } elseif ($settings['script'] == "popup") {
        $result_final = "<a href='javascript:;' onClick=\"openWin('{$image_url}{$photo->photo_filename}'); return false\"><img src='{$image_url}/tb_{$photo->photo_filename}' border='0' alt='{$photo->photo_title}' class=\"thumbnail\" /></a>";
    }
    echo $result_final; 
}

function random_photo_nofx($tags=null) {
    $settings = $settings = Plugin::getAllSettings('gallery');
    $page = $settings['gallery_page'];
    $result_final = "";
    $image_url = URL_PUBLIC . "public/" . $settings['images_folder'];

    $PDO = Record::getConnection();
    $driver = strtolower($PDO->getAttribute(Record::ATTR_DRIVER_NAME));
    if ($driver == 'sqlite') {
        $rand_func = 'random()';
    } else {
        // should work with mysql and postgres
        $rand_func = 'rand()';
    }
    if ($tags != null) {
        $where = "WHERE photo_category in (SELECT id from " . TABLE_PREFIX . "category WHERE tags in (" . my_implode($tags) . "))";
    } else {
        $where = '';
    }

    $sql = "SELECT * FROM " . TABLE_PREFIX . "photos " . $where . " ORDER BY " . $rand_func;
    $stmt = $PDO->prepare($sql);
    $stmt->execute();

    $photo = $stmt->fetchObject();
	echo "<div style='text-align: center;'>";
	echo "<img src='{$image_url}{$photo->photo_filename}'>";
	echo "</div>";
}

/**
 * function my_implode()
 *
 * @param array $a
 * @return string
 *
 * Like implode, build a string with the array elements, but
 * separator is comma and strings are surrounded by double quotes
 */
function my_implode($a) {
    return '"' . implode('","', $a) . '"';
}